package com.terracloud.server.controller;

import com.terracloud.server.config.annotations.ApiOperation;
import com.terracloud.server.queue.util.TbCoreComponent;
import com.terracloud.server.service.log.UnifiedLogUtilExampleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * UnifiedLogUtil测试控制器
 * 提供测试UnifiedLogUtil功能的接口
 */
@RestController
@TbCoreComponent
@RequestMapping("/api/unified-log-util/test")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "UnifiedLogUtil测试", description = "UnifiedLogUtil测试API")
public class UnifiedLogUtilTestController extends BaseController {
    
    private final UnifiedLogUtilExampleService unifiedLogUtilExampleService;
    
    /**
     * 执行UnifiedLogUtil完整功能示例
     */
    @ApiOperation(value = "执行UnifiedLogUtil完整功能示例", 
               notes = "执行UnifiedLogUtil工具类的所有功能示例，生成各种类型的日志记录")
    @PreAuthorize("hasAuthority('TENANT_ADMIN')")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "示例执行成功",
                    content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = String.class))),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @RequestMapping(value = "/execute-complete-example", method = RequestMethod.POST, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> executeCompleteExample() {
        try {
            unifiedLogUtilExampleService.executeCompleteExample();
            return ResponseEntity.ok("UnifiedLogUtil完整功能示例执行完成");
        } catch (Exception e) {
            log.error("执行UnifiedLogUtil完整功能示例时发生错误", e);
            return ResponseEntity.internalServerError().body("执行示例时发生错误: " + e.getMessage());
        }
    }
}